<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_CourtController extends Zend_Controller_Action 
{
	

	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			if($_COOKIE[COOKIE_PRE.'uid']!=''){
				
				$this->_redirect(MY_URL);
				
			}else{
			
				$this->_redirect(MY_URL.'/buyer-auth-login');
				
			}
			
		}
		
	}
	
	public function preDispatch()
	{
		
		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());
		
	}
	
	public function allAction(){
		
		$casefl=new CFenlei('court_case');
		
		$casefl->fenleiLang='en_flname';
		
		$fenleiArray=$casefl->getAllFenlei();
		
		$casefl->getAll($fenleiArray,0);
		
		$fenleiString=str_replace(array('<div class="smallChildDiv"></div>','<div></div>'),array('',''),$casefl->fenleiString);

		$this->view->assign('fenleiString',$fenleiString);
		
	}
	
	public function mycourtAction(){
		
		$casefl=new CFenlei('court_case');
		
		$casefl->fenleiLang='en_flname';
		
		$fenleiArray=$casefl->getAllFenlei();
		
		$casefl->getAll($fenleiArray,0);
		
		$fenleiString=str_replace(array('<div class="smallChildDiv"></div>','<div></div>'),array('',''),$casefl->fenleiString);

		$this->view->assign('fenleiString',$fenleiString);
		
	}
	
	public function hiscourtAction(){
		
		$casefl=new CFenlei('court_case');
		
		$casefl->fenleiLang='en_flname';
		
		$fenleiArray=$casefl->getAllFenlei();
		
		$casefl->getAll($fenleiArray,0);
		
		$fenleiString=str_replace(array('<div class="smallChildDiv"></div>','<div></div>'),array('',''),$casefl->fenleiString);

		$this->view->assign('fenleiString',$fenleiString);
		
	}
	
	public function moreAction(){
		
		$itemid=$this->_request->getParam('itemid');

    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'court_case',true);
    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	$member=new CMember();
    	
    	if($itemArray[0]['defendant_uid']==$_COOKIE[COOKIE_PRE.'uid']){
    		
    		$this->_redirect(MY_URL.'/'.$this->view->userType.'-court-hismore?itemid='.$itemid);
    		
    	}else if($itemArray[0]['uid']==$_COOKIE[COOKIE_PRE.'uid']){
    		
    		$this->_redirect(MY_URL.'/'.$this->view->userType.'-court-edit?itemid='.$itemid);
    		
    	}
    	
    	
    	$userArray=$member->getUserDetails(array('uid'=>$itemArray[0]['defendant_uid']));
    	
    	$defendant_username=$userArray[0]['username'];
    	
    	$this->view->assign('defendant_username',$defendant_username);
    	
    	
    	$userArray=$member->getUserDetails(array('uid'=>$itemArray[0]['uid']));
    	
    	$complainant_username=$userArray[0]['username'];
    	
    	$this->view->assign('complainant_username',$complainant_username);
    	
    	
    	$this->view->assign('uid',$_COOKIE[COOKIE_PRE.'uid']);
		
		$jury=new CCourtJury();
    	
    	$juryArray=$jury->getDetail($itemid,$_COOKIE[COOKIE_PRE.'uid']);
    	
    	$this->view->assign('juryArray',$juryArray);
		
	}
	
	public function addAction(){
		
		require_once (MY_ROOT."/fckeditor/fckeditor.php");
		
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		
		$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
		
		$oFCKeditor->Value		= '';
		
		$oFCKeditor->Height=400;
		
		$oFCKeditor->Width="98%";
		
		$this->view->assign('oFCKeditor',$oFCKeditor);	
		
	}
	
	public function doaddAction(){
		
    	$this->_helper->viewRenderer->setNoRender();
    	
        if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
		$member=new CMember();
		
		$userArray=$member->getUserDetails(array('username'=>$input->defendant_username));
		
		if(count($userArray)==0){
			
			$this->view->showMsg("This user is not exist!");
			
			return false;
			
		}
		
		/*插入到条目表 start*/
		$item=new CItem();
		$itemData=array(
						'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
						'flid'=>1,
						'subject'=>$input->court_name,
						'item_type'=>'court_case',
						'add_time'=>time(),
						'update_time'=>time(),
						'is_banned'=>'0',
				  );
		$itemid=$item->createItem($itemData);
    	/*插入到条目表 end*/
		
		
		/*图片附件 start*/
		if(count($thisArray['imgUrl'])>0){
			$resultArray=$this->addAttachment($input,$itemid);
			$attachmentData=$resultArray[0];
			$attachment=new CAttachment();
			$attachment->createAttachments($attachmentData);
		}
		/*图片附件 end*/
		
		$content=htmlspecialchars_decode($input->FCKeditor1);
		
		$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
		
    	/*插入到案件表 start*/
		$case=new CCase();
		$caseData=array(
						'itemid'=>$itemid,
						'product_url'=>$input->product_url,
						'defendant_uid'=>$userArray[0]['uid'],
						'case_intro'=>$input->case_intro,	
						'complainant_content'=>$content,	
						'defendant_content'=>'',
						'conclusion'=>'',
						'compensation'=>$input->compensation
					 );	
		$case->createCase($caseData);
		/*插入到案件表 end*/
		
		$this->view->showMsg("Post success!",'adminb-court-mycourt');
		
		return true;
		
	}
	
    public function addAttachment($input,$itemid){
    	
    	$attachmentData=array();
		
		$path=new CPath();
		
		$image=new CImage();
		
		Zend_Loader::loadClass('CFile');
		
		for($i=0;$i<count($input->imgUrl);$i++){
			
			$newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径
			
			CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹
			
			$cutPath=$image->cutPhoto($newPath, '200', $path->getPathFromType('cut_item'));//截图
			
			$attachmentData[$i]['itemid']=$itemid;
			
			$attachmentData[$i]['attachment_url']=$newPath;
			
			$attachmentData[$i]['cut_url']=$path->toDbPath('cut_item',$cutPath);
			
			$attachmentData[$i]['intro']=$input->imgIntro[$i];
			
			$attachmentData[$i]['uid']=$_COOKIE[COOKIE_PRE.'uid'];
			
		}
		
		return array($attachmentData);
    	
    }
    
    public function editAction(){
    	
		$itemid=$this->_request->getParam('itemid');

    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'court_case',true);
    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	$member=new CMember();
    	
    	$userArray=$member->getUserDetails(array('uid'=>$itemArray[0]['defendant_uid']));
    	
    	$defendant_username=$userArray[0]['username'];
    	
    	$this->view->assign('defendant_username',$defendant_username);
    	
    	$userArray=$member->getUserDetails(array('uid'=>$itemArray[0]['uid']));
    	
    	$complainant_username=$userArray[0]['username'];
    	
    	$this->view->assign('complainant_username',$complainant_username);
    	
    	$attachment=new CAttachment();
    	
    	$attachmentArray=$attachment->getAttachments($itemid,"uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
    	
    	$this->view->assign('attachmentArray',$attachmentArray);
    	
		require_once (MY_ROOT."/fckeditor/fckeditor.php");
		
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		
		$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
		
		$oFCKeditor->Value		= $itemArray[0]['complainant_content'];
		
		$oFCKeditor->Height=400;
		
		$oFCKeditor->Width="98%";
		
		$this->view->assign('oFCKeditor',$oFCKeditor);	
    	
    }
    
    public function doeditAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		
		$paramArray=$this->_request->getParams();
		
		$itemid=$paramArray['itemid'];
		
		$item=new CItem();
		
		$itemArray=$item->getDetail($itemid,'court_case',false);
    	
    	if($itemArray[0]['flid']!='1'){
    		
    		$this->view->showMsg("The case has been approved, and can not be modified! ");
    		
    		return false;
    		
    	}
		
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $paramArray);	
		
		$member=new CMember();
		
		$userArray=$member->getUserDetails(array('username'=>$input->defendant_username));
		
		if(count($userArray)==0){
			
			$this->view->showMsg("This user is not exist!");
			
			return false;
			
		}
		
		/*插入到条目表 start*/

		$itemData=array(
						'uid'=>$_COOKIE[COOKIE_PRE.'uid'],
						'flid'=>1,
						'subject'=>$input->court_name,
						'item_type'=>'court_case',
						'update_time'=>time(),
						'is_banned'=>'0',
				  );
		$item->editItem($itemid,$itemData);
    	/*插入到条目表 end*/
		
		
		/*图片附件 start*/
		$resultArray=$this->editAttachment($input,$itemid);
		$attachmentData=$resultArray[0];
		$attachment=new CAttachment();
		$attachment->editAttachments($attachmentData);
		/*图片附件 end*/
		
		$content=htmlspecialchars_decode($input->FCKeditor1);
		
		$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
		
    	/*插入到案件表 start*/
		$case=new CCase();
		$caseData=array(
						'product_url'=>$input->product_url,
						'defendant_uid'=>$userArray[0]['uid'],
						'case_intro'=>$input->case_intro,	
						'complainant_content'=>$content,
						'compensation'=>$input->compensation
					 );	
		$case->editCase($itemid,$caseData);
		/*插入到案件表 end*/
		
		$this->view->showMsg("Edit success!",'adminb-court-mycourt');
		
		return true;
    	
    }
    
    public function editAttachment($input,$itemid){
    	
    	$attachmentData=array();
		
		$path=new CPath();
		
		$image=new CImage();
		
		Zend_Loader::loadClass('CFile');
		
		$cut_url='';
		
		for($i=0;$i<count($input->imgUrl);$i++){
			
			$attachmentData[$i]['attachmentid']=$input->attachmentid[$i];
			
			if($attachmentData[$i]['attachmentid']==''){
				
				$newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径
				
				CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹
				
				$cutPath=$image->cutPhoto($newPath, '200', $path->getPathFromType('cut_item'));//截图	

				$cut_url=$path->toDbPath('cut_item',$cutPath);
				
				$attachmentData[$i]['attachment_url']=$newPath;
			
				$attachmentData[$i]['cut_url']=$cut_url;
				
				$attachmentData[$i]['itemid']=$itemid;
				
				$attachmentData[$i]['uid']=$_COOKIE[COOKIE_PRE.'uid'];
				
			}
			
			$attachmentData[$i]['intro']=$input->imgIntro[$i];


			
		}
		
		return array($attachmentData);
    	
    }

    
    public function getitemajaxAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$flid=$this->_request->getParam('flid');
    	
    	$page=$this->_request->getParam('page',1);
    	
    	$searchQuery=$this->_request->getParam('searchQuery','');
    	
    	$searchArray=json_decode(stripslashes($searchQuery),true);
    	
    	
    	$item=new CItem();
    		
        if($searchArray['who']=='my'){
    		
    		$itemCounts=$item->getAllItemCounts('court_case',$flid,$_COOKIE[COOKIE_PRE.'uid']);
    		
    	}else if($searchArray['who']=='his'){
    		
    		$itemCounts=$item->getAllItemCounts('court_case',$flid,0,'has_banned',"defendant_uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
    		
    	}else{
    		
    		$itemCounts=$item->getAllItemCounts('court_case',$flid,0);
    		
    	}
		
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 10 );
    	
    	if($searchArray['who']=='my'){
    	
			$itemArray=$item->getAllItems($pageArray['from'],10,'court_case',$flid,$_COOKIE[COOKIE_PRE.'uid']);
    	
    	}else if($searchArray['who']=='his'){
    		
    		$itemArray=$item->getAllItems($pageArray['from'],10,'court_case',$flid,0,'has_banned',"defendant_uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
    		
    	}else{
    		
    		$itemArray=$item->getAllItems($pageArray['from'],10,'court_case',$flid,0);
    		
    	}
			
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::commonDivAjax($pageArray ['pageNumber'], $pageArray ['page'], 'productEffection.showAll',$flid); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );
		
		$this->view->assign('itemArray',$itemArray);
		
		$this->view->assign('who',$searchArray['who']);
    	
    }
    
    public function uploadimgAction(){
    	
    	header("Content-type: text/html; charset=utf-8");
    	
    	$this->getHelper ('layout')->disableLayout ();
    	  	
    	$upload=new CUpload();
    	
    	$path=new CPath();
    	
    	$thisPath=$path->getPathFromType('temp');
	
    	$imageArray=$upload->uploadFile($thisPath,null,null,'500000');
    	
    	$isSuccess=false;
    	
    	if(is_array($imageArray)){
    		
    		$isSuccess=true;
    		
	    	$imgUrl=$path->toDbPath('temp',$imageArray[0]);
	    	
	    	$this->view->assign('imgUrl',$imgUrl);
	    	
    	}else{
    		
    		$error=$imageArray;
    		
    		$this->view->assign('error',$error);
    		
    	}
    	
    	$this->view->assign('isSuccess',$isSuccess);	
    	
    }
    
    public function delimgAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(unlink(MY_ROOT.'/'.$this->_request->getParam('imgurl'))){
    		
    		echo 'success';
    		
    	}else{
    		
    		echo 'error';
    		
    	}
    	
    }
    
    public function delimgbyidAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
		
		$this->getHelper ('layout')->disableLayout ();
    	
    	$attachmentid=$this->_request->getParam('attachmentid');
    	
    	$is_cut_url=$this->_request->getParam('is_cut_url');//当is_cut_url不为1即表明用户要删除的图片是截图
    	
    	$attachment=new CAttachment();

    	$attachmentArray=$attachment->getAttachmentByAttachmentid($attachmentid);
    	
    	$attachment_url=$attachmentArray['attachment_url'];
    	
    	$cut_url=$attachmentArray['cut_url'];
    	
    	$itemid=$attachmentArray['itemid'];
    	
    	$path=new CPath();
    	
    	@unlink($path->toAbsoluteRootPath($attachment_url));
    	
    	@unlink($path->toAbsoluteRootPath($cut_url));
    	
    	if($is_cut_url!=''){
    		
			$product=new CProduct();
			
			$product->editProduct($itemid,array('cut_url'=>''));
    		
    	}
    	
    	$attachment->delAttachmentsById(array($attachmentid));

    	echo 'success';

    	
    }
    
    public function dodelAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$item=new CItem();	
    	
    	if($itemid==''){
    		
    		$itemidArray=$this->_request->getParam('itemArray');
    		
    	}else{
    		
    		$itemArray=$item->getDetail($itemid,'court_case',false);
    	
	    	if($itemArray[0]['flid']!='1'){
	    		
	    		$this->view->showMsg("The case has been approved, and can not be modified! ");
	    		
	    		return false;
	    		
	    	}
    		
    		$itemidArray=array($itemid);
    		
    	}
    	
    	
    	$case=new CCase();
    	
    	$item=new CItem();
    	
    	$case->delCases($itemidArray);
    	
    	$item->delItems($itemidArray);
    	
    	$attachment=new CAttachment();
    	
    	$attachment->delAttachmentsByItemid($itemidArray);
    	
    	$this->view->showMsg('删除成功!');
    	
    	return true;
    	
    }
    
    public function hismoreAction(){
    	
    	$itemid=$this->_request->getParam('itemid');

    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'court_case',true);
    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	$member=new CMember();
    	
    	$userArray=$member->getUserDetails(array('uid'=>$itemArray[0]['defendant_uid']));
    	
    	$defendant_username=$userArray[0]['username'];
    	
    	$this->view->assign('defendant_username',$defendant_username);
    	
    	$userArray=$member->getUserDetails(array('uid'=>$itemArray[0]['uid']));
    	
    	$complainant_username=$userArray[0]['username'];
    	
    	$this->view->assign('complainant_username',$complainant_username);
    	
    	$attachment=new CAttachment();
    	
    	$attachmentArray=$attachment->getAttachments($itemid,"uid='".$_COOKIE[COOKIE_PRE.'uid']."'");
    	
    	$this->view->assign('attachmentArray',$attachmentArray);
    	
    	require_once (MY_ROOT."/fckeditor/fckeditor.php");
		
		$oFCKeditor = new FCKeditor('FCKeditor1') ;
		
		$oFCKeditor->BasePath	= ROOT_PATH.'/fckeditor/' ;
		
		$oFCKeditor->Value		= $itemArray[0]['defendant_content'];
		
		$oFCKeditor->Height=400;
		
		$oFCKeditor->Width="98%";
		
		$this->view->assign('oFCKeditor',$oFCKeditor);	
    	
    }
    
    public function prosecutionAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'court_case',false);
    	
    	echo $itemArray[0]['complainant_content'];
    	
    }
    
    public function defendantAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'court_case',false);
    	
    	echo $itemArray[0]['defendant_content'];
    	
    }
    
	public function conclusionAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$item=new CItem();
    	
    	$itemArray=$item->getDetail($itemid,'court_case',false);
    	
    	echo $itemArray[0]['conclusion'];
    	
    }
	
    public function doconfirmAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$content=$this->_request->getParam('FCKeditor1');
    	
    	$itemid=$this->_request->getParam('itemid');

		$content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
		
		$paramArray=$this->_request->getParams();
		
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $paramArray);	
		
		/*图片附件 start*/
		$resultArray=$this->editAttachment($input,$itemid);
		$attachmentData=$resultArray[0];
		$attachment=new CAttachment();
		$attachment->editAttachments($attachmentData);
		/*图片附件 end*/
		
    	/*插入到案件表 start*/
		$case=new CCase();
		$caseData=array(
						'defendant_content'=>$content,
					 );	
		$case->editCase($itemid,$caseData);
		/*插入到案件表 end*/
		
		$this->view->showMsg("Confirm success!",'adminb-court-hiscourt');
    	
    }
    
    public function joinjuryAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		
		$thisArray=$this->_request->getParams();
		
		$item=new CItem();
		
		$itemArray=$item->getDetail($thisArray['itemid'],'court_case',false);

		if($itemArray[0]['uid']==$_COOKIE[COOKIE_PRE.'uid']||$itemArray[0]['defendant_uid']==$_COOKIE[COOKIE_PRE.'uid']){
			
			$this->view->showMsg("不能申请！",$_SERVER['HTTP_REFERER']);
			
			return false;
			
		}
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
		
		$jury=new CCourtJury();
		
		$data=array(
					'itemid'=>$thisArray['itemid'],
					'jury_uid'=>$_COOKIE[COOKIE_PRE.'uid'],
					'reason'=>$input->reason,
					'comment'=>'',
					'add_time'=>time(),
					);
		try{
			
			$jury->createJury($data);
			
			$this->view->showMsg("申请成功！请耐心等待申请结果！",$_SERVER['HTTP_REFERER']);
			
		}catch(Exception $e){
			
			$this->view->showMsg("你已经申请过了！",$_SERVER['HTTP_REFERER']);
			
		}
		
		return true;
    	
    }
    
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
